package com.capgemini.fs.presentation.user.web.action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Group;
import com.capgemini.fs.app.ekms.model.Role;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.app.ekms.service.RoleService;
import com.capgemini.fs.app.ekms.service.UserService;
import com.capgemini.fs.presentation.user.web.form.ModifyUserFowardForm;

public class ModifyUserFowardAction extends Action {

	private UserService modifyUserFowardService;
	private RoleService modifyUserFowardRoleService;
	private GroupService modifyUserFowardGroupService;

	public GroupService getModifyUserFowardGroupService() {
		return modifyUserFowardGroupService;
	}

	public void setModifyUserFowardGroupService(
			GroupService modifyUserFowardGroupService) {
		this.modifyUserFowardGroupService = modifyUserFowardGroupService;
	}

	public RoleService getModifyUserFowardRoleService() {
		return modifyUserFowardRoleService;
	}

	public void setModifyUserFowardRoleService(
			RoleService modifyUserFowardRoleService) {
		this.modifyUserFowardRoleService = modifyUserFowardRoleService;
	}

	public UserService getModifyUserFowardService() {
		return modifyUserFowardService;
	}

	public void setModifyUserFowardService(UserService modifyUserFowardService) {
		this.modifyUserFowardService = modifyUserFowardService;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ModifyUserFowardForm form1 = (ModifyUserFowardForm) form;

		User candidateFoward = modifyUserFowardService.getUserById(form1
				.getUserId());
		HttpSession session1 = request.getSession();
		session1.setAttribute("userForwardId", form1.getUserId());

		// set action to UI
		if (Constants.USER_STATUS_ACTIVE.equals(candidateFoward.getStatus())) {
			request.setAttribute("yes1", 1);
		} else {
			request.setAttribute("no1", 1);
		}
		// set category data
		if (Constants.USER_MEMBERTYPE_TRAINEE.equals(candidateFoward
				.getMemberType())) {
			request.setAttribute("yes11", 1);
			request.setAttribute("yes2", 1);
			request.setAttribute("trainee", 1);
		} else {
			request.setAttribute("no22", 1);
			request.setAttribute("no2", 1);
		}

		// set candidate object to UI
		request.setAttribute("info", candidateFoward);

		// set role to UI
		List<Role> role = modifyUserFowardRoleService.getAllRoles();
		HttpSession session = request.getSession();
		session.setAttribute("roleList", role);

		// set which role the user has
		Set<Role> roles = candidateFoward.getRoles();
		System.out.println(roles.size());
		Iterator<Role> i = roles.iterator();
		while (i.hasNext()) {
			Role currentRoleI = i.next();
			for (int n = 0; n < role.size(); n++) {
				if (currentRoleI.getName().equalsIgnoreCase(
						role.get(n).getName())) {
					String matchRole = currentRoleI.getName();//
					request.setAttribute(matchRole, 1);
				}
			}
		}
		// set group to UI
		List<Group> group = modifyUserFowardGroupService.findAllGroup();
		session.setAttribute("groupList", group);

		// set which group the user has
		Set<Group> groups = candidateFoward.getGroups();
		if (groups != null) {
			Iterator<Group> j = groups.iterator();
			while (j.hasNext()) {
				Group currentGroupI = j.next();
				for (int n = 0; n < group.size(); n++) {
					if (currentGroupI.getGroupName().equalsIgnoreCase(
							group.get(n).getGroupName())) {
						String matchGroup = currentGroupI.getGroupName();//
						request.setAttribute(matchGroup, 1);
					}
				}
			}
		}

		return mapping.findForward("suc");
	}
}
